home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
networking
/
ip
/
ka9q
/
MNetsrc.hqx
/
Mac TCP_IP Source v.33
/
Other Files.SIT
/
8250.c
next >
Wrap
Text File
|
1989-01-13
|
7KB
|
327 lines
/* OS- and machine-dependent stuff for the 8250 asynch chip on a IBM-PC */
#include <stdio.h>
#include "global.h"
#include "asy.h"
#include "8250.h"
#include "iface.h"
struct asy asy[ASY_MAX];
unsigned nasy;
/* ASY interrupt handlers */
extern void asy0vec(),asy1vec(),asy2vec(),asy3vec(),asy4vec();
void (*handle[])() = {asy0vec,asy1vec,asy2vec,asy3vec,asy4vec};
/* Initialize asynch port "dev" */
int
asy_init(dev,arg1,arg2,bufsize)
int16 dev;
char *arg1,*arg2; /* Attach args for address and vector */
unsigned bufsize;
{
register unsigned base;
register struct fifo *fp;
register struct asy *ap;
void (*getirq())();
char i_state;
ap = &asy[dev];
ap->addr = htoi(arg1);
ap->vec = htoi(arg2);
/* Set up receiver FIFO */
fp = &ap->fifo;
if((fp->buf = malloc(bufsize)) == NULLCHAR){
printf("asy%d: No space for rx buffer\r\n");
fflush(stdout);
return;
}
fp->bufsize = bufsize;
fp->wp = fp->rp = fp->buf;
fp->cnt = 0;
base = ap->addr;
/* Purge the receive data buffer */
(void)inportb(base+RBR);
i_state = disable();
/* Save original interrupt vector, mask state, control bits */
ap->save.vec = getirq(ap->vec);
ap->save.mask = getmask(ap->vec);
ap->save.lcr = inportb(base+LCR);
ap->save.ier = inportb(base+IER);
ap->save.mcr = inportb(base+MCR);
/* save speed bytes */
setbit(base+LCR,LCR_DLAB);
ap->save.divl = inportb(base+DLL);
ap->save.divh = inportb(base+DLM);
clrbit(base+LCR,LCR_DLAB);
/* Set interrupt vector to SIO handler */
setirq(ap->vec,handle[dev]);
/* Set line control register: 8 bits, no parity */
outportb(base+LCR,(char)LCR_8BITS);
/* Turn on receive interrupt enable in 8250, leave transmit
* and modem status interrupts turned off for now
*/
outportb(base+IER,(char)IER_DAV);
/* Set modem control register: assert DTR, RTS, turn on 8250
* master interrupt enable (connected to OUT2)
*/
outportb(base+MCR,(char)(MCR_DTR|MCR_RTS|MCR_OUT2));
/* Enable interrupt */
maskon(ap->vec);
restore(i_state);
}
int
asy_stop(iface)
struct interface *iface;
{
register unsigned base;
register struct asy *ap;
char i_state;
ap = &asy[iface->dev];
base = ap->addr;
/* Purge the receive data buffer */
(void)inportb(base+RBR);
/* Restore original interrupt vector and 8259 mask state */
i_state = disable();
setirq(ap->vec,ap->save.vec);
if(ap->save.mask)
maskon(ap->vec);
else
maskoff(ap->vec);
/* Restore speed regs */
setbit(base+LCR,LCR_DLAB);
outportb(base+DLL,ap->save.divl); /* Low byte */
outportb(base+DLM,ap->save.divh); /* Hi byte */
clrbit(base+LCR,LCR_DLAB);
/* Restore control regs */
outportb(base+LCR,ap->save.lcr);
outportb(base+IER,ap->save.ier);
outportb(base+MCR,ap->save.mcr);
restore(i_state);
}
/* Asynchronous line I/O control */
asy_ioctl(interface,argc,argv)
struct interface *interface;
int argc;
char *argv[];
{
if(argc < 1){
printf("%d\r\n",asy[interface->dev].speed);
return 0;
}
return asy_speed(interface->dev,atoi(argv[0]));
}
/* Set asynch line speed */
int
asy_speed(dev,speed)
int16 dev;
int speed;
{
register unsigned base;
register int divisor;
char i_state;
if(speed == 0 || dev >= nasy)
return -1;
base = asy[dev].addr;
asy[dev].speed = speed;
divisor = BAUDCLK / (long)speed;
i_state = disable();
/* Purge the receive data buffer */
(void)inportb(base+RBR);
/* Turn on divisor latch access bit */
setbit(base+LCR,LCR_DLAB);
/* Load the two bytes of the register */
outportb(base+DLL,(char)(divisor & 0xff)); /* Low byte */
outportb(base+DLM,(char)((divisor >> 8) & 0xff)); /* Hi byte */
/* Turn off divisor latch access bit */
clrbit(base+LCR,LCR_DLAB);
restore(i_state);
return 0;
}
/* Send a buffer to serial transmitter */
asy_output(dev,buf,cnt)
unsigned dev;
char *buf;
unsigned short cnt;
{
register struct dma *dp;
unsigned base;
char i_state;
if(dev >= nasy)
return;
base = asy[dev].addr;
dp = &asy[dev].dma;
i_state = disable();
if(dp->flags){
restore(i_state);
return; /* Already busy */
}
dp->data = buf;
dp->cnt = cnt;
dp->flags = 1;
/* Enable transmitter buffer empty interrupt and simulate
* an interrupt; this will get things rolling.
*/
setbit(base+IER,IER_TxE);
asytxint(dev);
restore(i_state);
}
/* Receive characters from asynch line
* Returns count of characters read
*/
int16
asy_recv(dev,buf,cnt)
int16 dev;
char *buf;
unsigned cnt;
{
unsigned tot,n;
int kbread();
char i_state;
struct fifo *fp;
fp = &asy[dev].fifo;
tot = 0;
/* Read from serial I/O input buffer */
i_state = disable();
for(;;){
n = min(cnt,fp->cnt);
if(n == 0)
break;
n = min(n,&fp->buf[fp->bufsize] - fp->rp);
memcpy(buf,fp->rp,n);
fp->rp += n;
if(fp->rp >= &fp->buf[fp->bufsize])
fp->rp = fp->buf;
fp->cnt -= n;
buf += n;
tot += n;
cnt -= n;
}
restore(i_state);
return tot;
}
/* Interrupt handler for 8250 asynch chip */
void
asyint(dev)
unsigned dev;
{
register unsigned base;
register char iir;
base = asy[dev].addr;
while(((iir = inportb(base+IIR)) & IIR_IP) == 0){
switch(iir & IIR_ID){
case IIR_RDA: /* Receiver interrupt */
asyrxint(dev);
break;
case IIR_THRE: /* Transmit interrupt */
asytxint(dev);
break;
}
}
}
/* Process 8250 receiver interrupts */
static
asyrxint(dev)
unsigned dev;
{
unsigned base;
register struct fifo *fp;
char c;
base = asy[dev].addr;
fp = &asy[dev].fifo;
while(inportb(base+LSR) & LSR_DR){
c = inportb(base+RBR);
/* Process incoming data;
* If buffer is full, we have no choice but
* to drop the character
*/
if(fp->cnt != fp->bufsize){
*fp->wp++ = c;
if(fp->wp == &fp->buf[fp->bufsize])
/* Wrap around */
fp->wp = fp->buf;
fp->cnt++;
}
}
}
/* Handle 8250 transmitter interrupts */
static
asytxint(dev)
unsigned dev;
{
register struct dma *dp;
register unsigned base;
base = asy[dev].addr;
dp = &asy[dev].dma;
if(!dp->flags){
/* "Shouldn't happen", but disable transmit
* interrupts anyway
*/
clrbit(base+IER,IER_TxE);
return; /* Nothing to send */
}
while(inportb(base+LSR) & LSR_THRE){
outportb(base+THR,*dp->data++);
if(--dp->cnt == 0){
dp->flags = 0;
/* Disable transmit interrupts */
clrbit(base+IER,IER_TxE);
/* Call completion interrupt here */
break;
}
}
}
int
stxrdy(dev)
int16 dev;
{
return(!asy[dev].dma.flags);
}
/* Set bit(s) in I/O port */
setbit(port,bits)
unsigned port;
char bits;
{
outportb(port,(char)inportb(port)|bits);
}
/* Clear bit(s) in I/O port */
clrbit(port,bits)
unsigned port;
char bits;
{
outportb(port,(char)(inportb(port) & ~bits));
}